/*
 * Copyright (c) 2006-2024, RT-Thread Development Team
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 * Change Logs:
 * Date           Author       Notes
 * 2024-11-29     RT-Thread    first version
 */

#include <rtthread.h>
#include <board.h>

#define DBG_TAG "main"
#define DBG_LVL DBG_LOG
#include <rtdbg.h>

#include <rtdevice.h>
#include <fal.h>

#include <qled_sample.h>
#include <easyflash.h>

#include "user_config.h"


extern void G_QledInit(rt_uint8_t flag);



user_config_type get_config_data;
rt_sem_t test_sem1 = RT_NULL;





/*********************************************************************************/
void Peripheral_power_ctrl(int powermode) //flash模块供电
{
    rt_pin_mode(Power_PP2_ctrl_pin_num, PIN_MODE_OUTPUT);
    if (Power_on == powermode)
    {
        rt_pin_write(Power_PP2_ctrl_pin_num, 1);
    }
    else
    {
        rt_pin_write(Power_PP2_ctrl_pin_num, 0);
    }
}
/*********************************************************************************/

/*********************************************************************************/
/* 环境变量载入
 */
void env_load(void)
{
    char temp[25] = {0};
    unsigned int len = 0;

    ef_get_env_blob("HardwareVersion",RT_NULL, 0, &len);
    if(len > 15)
    {
        LOG_E("HardwareVersion get env too lengthy");
        return;
    }
    ef_get_env_blob("HardwareVersion", temp, len, RT_NULL);
    get_config_data.HardwareVersion=atoi(temp);
    rt_memset(temp, 0, 25);

    ef_get_env_blob("SoftwareVersion",RT_NULL, 0, &len);
    if(len > 15)
    {
        LOG_E("SoftwareVersion get env too lengthy");
        return;
    }
    ef_get_env_blob("SoftwareVersion", temp, len, RT_NULL);
    get_config_data.SoftwareVersion = atoi(temp);
    rt_memset(temp, 0, 25);


    ef_get_env_blob("testdata1",RT_NULL, 0, &len);
    if(len > 15)
    {
        LOG_E("testdata1 get env too lengthy");
        return;
    }
    ef_get_env_blob("testdata1", temp, len, RT_NULL);
    get_config_data.testdata1 = atoi(temp);
    rt_memset(temp, 0, 25);

    ef_get_env_blob("testdata2",RT_NULL, 0, &len);
    if(len > 15)
    {
        LOG_E("testdata2 get env too lengthy");
        return;
    }
    ef_get_env_blob("testdata2", temp, len, RT_NULL);
    get_config_data.testdata2 = atoi(temp);
    rt_memset(temp, 0, 25);

    LOG_D("---------------ENV-------------------");
    LOG_D("Heartbeat:%d",            get_config_data.Heartbeat);
    LOG_D("Sending_interval_val:%d", get_config_data.Sending_interval_val);
    LOG_D("SoftwareVersion:%d",      get_config_data.SoftwareVersion);
    LOG_D("HardwareVersion:%d",      get_config_data.HardwareVersion);
    LOG_D("testdata1:%d",            get_config_data.testdata1);
    LOG_D("testdata2:%d",            get_config_data.testdata2);
    LOG_D("-------------------------------------");
}
/*********************************************************************************/

/*********************************************************************************/
void G_SetEnv_entry(void)
{
    char chartemp[15];
    int data1 = 20;
    int data2 = 11;

    while(1)
    {
        rt_memset(chartemp, 0, sizeof(chartemp));
        itoa(++data1, chartemp, 10);
        ef_set_env("testdata1", chartemp); //存入env中
        LOG_I("write testdata1: %d", data1);

        rt_memset(chartemp, 0, sizeof(chartemp));
        itoa(data2 += 2, chartemp, 10);
        ef_set_env("testdata2", chartemp); //存入env中
        LOG_I("write testdata2: %d", data2);

        rt_sem_release(test_sem1);

        rt_thread_delay(2000);
    }
}
/*********************************************************************************/

/*********************************************************************************/
void G_ReadEnv_entry(void)
{
    char temp[25] = {0};
    unsigned int len = 0;

    while(1)
    {
        rt_sem_take(test_sem1, RT_WAITING_FOREVER);

        ef_get_env_blob("testdata1",RT_NULL, 0, &len);
        if(len > 15)
        {
            LOG_E("testdata1 get env too lengthy");
    //            return;
        }
        ef_get_env_blob("testdata1", temp, len, RT_NULL);
        get_config_data.testdata1 = atoi(temp);
        LOG_I("read testdata1: %d", get_config_data.testdata1);
        rt_memset(temp, 0, 25);


        ef_get_env_blob("testdata2", RT_NULL, 0, &len);
        if(len > 15)
        {
            LOG_E("testdata2 get env too lengthy");
    //            return;
        }
        ef_get_env_blob("testdata2", temp, len, RT_NULL);
        get_config_data.testdata2 = atoi(temp);
        LOG_I("read testdata2: %d", get_config_data.testdata2);
        rt_memset(temp, 0, 25);

        rt_thread_delay(5);
    }
}
/*********************************************************************************/

/*********************************************************************************/






int main(void)
{
    int count = 1;


    fal_init();       //fal初始化
    easyflash_init(); //easyflash初始化

    env_load();       //环境变量载入

    test_sem1 = rt_sem_create("sem1", 0, RT_IPC_FLAG_FIFO);

    rt_thread_t thread1 = rt_thread_create("set_env", (void *)G_SetEnv_entry, RT_NULL, 2048, 25, 10);
    if (thread1 != RT_NULL)
    {
        rt_thread_startup(thread1);
    }

    rt_thread_t thread2 = rt_thread_create("read_env", (void *)G_ReadEnv_entry, RT_NULL, 1024, 25, 10);
    if (thread2 != RT_NULL)
    {
        rt_thread_startup(thread2);
    }


    G_QledInit(1);

    while (count++)
    {
        LOG_D("Hello RT-Thread!");
        rt_thread_mdelay(1000);
    }

    return RT_EOK;
}
